package cn.rongdao.jrkabao.ui.information;

import android.support.v4.content.ContextCompat;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.mifengkong.frtools.http.request.callback.FRBaseObserver;
import com.mifengkong.frtools.util.FRSharedPreferences;
import com.mifengkong.frtools.util.FRString;
import com.mifengkong.frtools.util.FRToast;
import com.mifengkong.frwidget.titlebar.TitleBarUIComponent;

import java.util.HashMap;
import java.util.Map;

import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.base.FRBaseLooperRequestActivity;
import cn.rongdao.jrkabao.bean.takephoto.ValidateIDCardInfoBean;
import cn.rongdao.jrkabao.constant.SPConstant;
import cn.rongdao.jrkabao.model.information.AddCridetCardNumberModel;
import cn.rongdao.jrkabao.model.takephoto.IdCardNameAndNumberModel;
import cn.rongdao.jrkabao.util.listener.OnTextChangeListener;
import cn.rongdao.jrkabao.widget.CardEditText;

/**
 * Created by jiangyongxing on 2016/8/25.
 * 描述：添加信用卡号的界面
 */
public class AddCridetCardActivity extends FRBaseLooperRequestActivity implements View.OnClickListener {

    public static final int RESULT_ADD_CRIDET_CARD_ACTIVITY = 1;
    private TitleBarUIComponent titleBarUIComponent;
    private TextView mName;
    private CardEditText number;
    private Button submit;
    private String numberStr;
    private Map<String, String> mMap;
    private String mNameStr;


    @Override
    protected int layoutRes() {
        return R.layout.activity_add_cridet_card;
    }

    @Override
    protected void initView() {
        super.initView();
        initData();
        titleBarUIComponent = (TitleBarUIComponent) findViewById(R.id.aacc_titlebar);
        initTitleBar();
        mName = (TextView) findViewById(R.id.aacc_name);
        number = (CardEditText) findViewById(R.id.aacc_number);
        submit = (Button) findViewById(R.id.aacc_submit);
        submit.setOnClickListener(this);
        TextView mSkip = (TextView) findViewById(R.id.aacc_skip);
        mSkip.setOnClickListener(this);
        number.addTextChangedListener(new OnTextChangeListener() {
            @Override
            public void afterTextChanged(Editable s) {
                if (!FRString.isEmpty(number.getContentString())) {
                    if (number.getContentString().length() == 16) {
                        //表示为正确
                        submit.setBackgroundResource(R.drawable.orangebg_50radius);
                        submit.setClickable(true);
                    } else {
                        submit.setBackgroundResource(R.drawable.graybg_50radius);
                        submit.setClickable(false);

                    }
                }
            }
        });

    }

    private void initTitleBar() {
        titleBarUIComponent.initTitleBlack("信用卡验证");
        titleBarUIComponent.setBackgroundColor(ContextCompat.getColor(this, R.color.white));
        titleBarUIComponent.setBottomViewVisiable(true);
        titleBarUIComponent.initLeftBackBlack(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    protected void initData() {
        mMap = new HashMap<>();
        getCardNameAndNumberData();
    }

    private void getCardNameAndNumberData() {
        IdCardNameAndNumberModel.getInstance().execute(new FRBaseObserver<ValidateIDCardInfoBean>(this) {
            @Override
            protected void onBaseNext(ValidateIDCardInfoBean data) {
                mNameStr = data.getReal_name();
                mName.setText(mNameStr);
                FRSharedPreferences.setStringDefault(SPConstant.ID_CARD, data.getIdentity_num());
                FRSharedPreferences.setStringDefault(SPConstant.NAME, mNameStr);
            }

            @Override
            protected void onBaseError(Throwable t) {

            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.aacc_submit:
                if (checkData()) {
                    mMap.put("bank_no", numberStr);
                    mMap.put("name", mNameStr);
                    AddCridetCardNumberModel.getInstance().execute(mMap, new FRBaseObserver<String>(this, "验证中，请稍候") {
                        @Override
                        protected void onBaseNext(String data) {
                            FRToast.showToastSafe("验证成功");
                            finish();
                        }
                    });
                }
                break;
            case R.id.aacc_skip:
                setResult(RESULT_ADD_CRIDET_CARD_ACTIVITY);
                finish();
                break;
        }
    }

    private boolean checkData() {
        numberStr = number.getContentString();
        if (FRString.isEmpty(numberStr)) {
            FRToast.showToastSafe("信用卡号不能为空");
            return false;
        }
        return true;
    }
}
